package cn.com.ddapp.ddm.ui;

import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.webkit.WebResourceError;
import android.webkit.WebResourceRequest;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;

import org.json.JSONObject;

import butterknife.BindView;
import butterknife.OnClick;
import cn.com.ddapp.ddm.R;
import cn.com.ddapp.ddm.api.OKUtilAPI;
import cn.com.ddapp.ddm.base.BaseActivity;
import cn.com.ddapp.ddm.inter.OkUtilCallback;
import cn.com.ddapp.ddm.ui.empty.EmptyLayout;
import cn.com.ddapp.ddm.util.NetUtils;
import cn.com.ddapp.ddm.util.SPUtils;
import cn.com.ddapp.ddm.util.StringUtils;
import cn.com.ddapp.ddm.util.TDevice;
import okhttp3.Call;

//关于我们
public class AboutActivity extends BaseActivity {
    @BindView(R.id.news_detail_content)
    WebView wv_news_detail_content;
    @BindView(R.id.empty)
    EmptyLayout emptyLayout;
    @BindView(R.id.back)
    ImageView back;
    @BindView(R.id.titletext)
    TextView titletext;
    String type;

    @Override
    public void initView() {
        if(TDevice.isTablet(getApplicationContext())){
            WebSettings settings = wv_news_detail_content.getSettings();
            // 设置自适应屏幕，两者合用
            settings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
            settings.setUseWideViewPort(true);
            settings.setLoadWithOverviewMode(true);
//            settings.setTextSize(WebSettings.TextSize.LARGER);//设置字体大小
            settings.setDefaultFontSize(20);
            settings.setJavaScriptEnabled(true);
//        wv_news_detail_content.setInitialScale(100);
            // 设置可以缩放
            settings.setSupportZoom(true);
            settings.setBuiltInZoomControls(true);

            settings.setDisplayZoomControls(false);

            wv_news_detail_content.setWebViewClient(new WebViewClient() {

                @Override
                public void onPageFinished(WebView view, String url) {
                    System.out.println("onPageFinished");
                    emptyLayout.setErrorType(EmptyLayout.HIDE_LAYOUT);
                    super.onPageFinished(view, url);
                }
                @Override
                public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
                    System.out.println("cuowu");
                    emptyLayout.setErrorType(EmptyLayout.NODATA);
                    super.onReceivedError(view, request, error);
                }
            });
        }else{
            WebSettings settings = wv_news_detail_content.getSettings();
            // 设置自适应屏幕，两者合用
            settings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
            settings.setUseWideViewPort(true);
            settings.setLoadWithOverviewMode(true);
//            settings.setTextSize(WebSettings.TextSize.LARGER);//设置字体大小
            settings.setDefaultFontSize(40);
            settings.setJavaScriptEnabled(true);
//        wv_news_detail_content.setInitialScale(100);
            // 设置可以缩放
            settings.setSupportZoom(true);
            settings.setBuiltInZoomControls(true);

            settings.setDisplayZoomControls(false);

            wv_news_detail_content.setWebViewClient(new WebViewClient() {

                @Override
                public void onPageFinished(WebView view, String url) {
                    System.out.println("onPageFinished");
                    emptyLayout.setErrorType(EmptyLayout.HIDE_LAYOUT);
                    super.onPageFinished(view, url);
                }
                @Override
                public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
                    System.out.println("cuowu");
                    emptyLayout.setErrorType(EmptyLayout.NODATA);
                    super.onReceivedError(view, request, error);
                }

            });
        }
        String categroy= getIntent().getBundleExtra("ABOUT").getString("category");
        if("1".equals(categroy)){
            titletext.setText("关于我们");
            type="1";
            httpData();
        }else if("2".equals(categroy)){
            titletext.setText("帮助说明");
            type="2";
            httpData();
        }else if("3".equals(categroy)){
            titletext.setText("活动规则");
            type="3";
            httpData();
        }else if("4".equals(categroy)){
            System.out.println("bannerlink="+getIntent().getBundleExtra("ABOUT").getString("data"));
           getdata(getIntent().getBundleExtra("ABOUT").getString("data"));
        }

    }

    private void getdata(String id) {
        emptyLayout.setErrorType(EmptyLayout.NETWORK_LOADING);
        if (!NetUtils.isConnected(this)) {
            Toast.makeText(this, "请检查你的网络", Toast.LENGTH_SHORT).show();
        } else {
            OkUtilCallback callback = new OkUtilCallback() {
                @Override
                public void onMyResponse(String response, int id) {
                    Gson gson = new Gson();
                    Log.e("respn", response);
                    try {
                        JSONObject jsonObject = new JSONObject(response);
                        if ("OK".equals(jsonObject.getString("result"))) {
                            if (!jsonObject.isNull("content")){
                                titletext.setText(jsonObject.getString("title"));
                                String content = jsonObject.getString("content");
                                wv_news_detail_content.loadData(content, "text/html;charset=UTF-8", null);
                            }
                            else {
                                emptyLayout.setErrorType(EmptyLayout.NODATA);
                            }
                        }else{
                        }
                    } catch (Exception e) {
                        emptyLayout.setErrorType(EmptyLayout.NETWORK_ERROR);
                    }
                }

                @Override
                public void onError(Call call, Exception e, int id) {
                    emptyLayout.setErrorType(EmptyLayout.NETWORK_ERROR);
                    super.onError(call, e, id);
                }
            };
            String userId = SPUtils.getStringData(this, SPUtils.USERID);

            //OKUtilAPI.post(OKUtilAPI.getART, "userId=" + userId+"&type="+type, callback);
            OKUtilAPI.post(OKUtilAPI.showArticle, "id=" + id, callback);
        }
    }

    @Override
    public void initData() {

    }

    private void httpData() {
        emptyLayout.setErrorType(EmptyLayout.NETWORK_LOADING);
        if (!NetUtils.isConnected(this)) {
            Toast.makeText(this, "请检查你的网络", Toast.LENGTH_SHORT).show();
        } else {
            OkUtilCallback callback = new OkUtilCallback() {
                @Override
                public void onMyResponse(String response, int id) {
                    Gson gson = new Gson();
                    Log.e("respn", response);
                    try {
                        JSONObject jsonObject = new JSONObject(response);
                        if ("OK".equals(jsonObject.getString("result"))) {
                            if (!jsonObject.isNull("content")){
                                String content = jsonObject.getString("content");
                                wv_news_detail_content.loadData(content, "text/html;charset=UTF-8", null);
                            }
                            else {
                                emptyLayout.setErrorType(EmptyLayout.NODATA);
                            }
                        }
                    } catch (Exception e) {
                        emptyLayout.setErrorType(EmptyLayout.NETWORK_ERROR);
                    }
                }

                @Override
                public void onError(Call call, Exception e, int id) {
                    emptyLayout.setErrorType(EmptyLayout.NETWORK_ERROR);
                    super.onError(call, e, id);
                }
            };
            String userId = SPUtils.getStringData(this, SPUtils.USERID);

            //OKUtilAPI.post(OKUtilAPI.getART, "userId=" + userId+"&type="+type, callback);
            OKUtilAPI.post(OKUtilAPI.getART, "type=" + type, callback);
        }
    }

    @Override
    public int getContentViewId() {
        return R.layout.activity_about;
    }

    @Override
    protected String getFragmentTag() {
        return null;
    }

    @OnClick({R.id.back,R.id.img_error_layout})
    public void onClick(View view) {
        Intent intent;
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.img_error_layout:
                initData();
                break;
        }
        super.onClick(view);
    }
}
